﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public static class LoaiKyNang
    {
        public static string ChuyenMon = "Chuyên môn";
        public static string NgoaiNgu = "Ngoại ngữ";
        public static string KyNangMem = "Kỹ năng mềm";
    }

    public class KyNangManager
    {
        public ActionResultAsList<KyNang> LayDS()
        {
            var ar = new ActionResultAsList<KyNang>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.KyNangs.Include("NhanViens").ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<KyNang> LayDSTheoMaKN(string maKN)
        {
            var ar = new ActionResultAsList<KyNang>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.KyNangs.Include("NhanViens").Where(k => k.MaKyNang == maKN).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<KyNang> LayDSTheoTenKN(string tenKN)
        {
            var ar = new ActionResultAsList<KyNang>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.KyNangs.Include("NhanViens").Where(k => k.TenKyNang == tenKN).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<KyNang> LayDSTheoLoaiKN(string loai)
        {
            var ar = new ActionResultAsList<KyNang>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.KyNangs.Include("NhanViens").Where(k => k.LoaiKyNang == loai).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<KyNang> LayDSTheoMaNV(string maNV)
        {
            var ar = new ActionResultAsList<KyNang>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.NhanViens.Include("KyNangs").Single(n => n.MaNhanVien == maNV).KyNangs.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResult Them(KyNang kn)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    List<KyNang> list = db.KyNangs.ToList();
                    if (list.Exists(k => k.MaKyNang == kn.MaKyNang))
                    {
                        ac.AddException(new Exception("Mã kỹ năng đã tồn tại: " + kn.MaKyNang));
                        return ac;
                    }
                    db.KyNangs.Add(kn);
                    db.SaveChanges();
                    kn = db.KyNangs.Include("Nhanviens").Single(k => k.MaKyNang == kn.MaKyNang);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult CapNhat(KyNang kn)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var kn1 = db.KyNangs.Single(k => k.MaKyNang == kn.MaKyNang);
                    kn1.TenKyNang = kn.TenKyNang;
                    kn1.LoaiKyNang = kn.LoaiKyNang;
                    kn1.MoTa = kn.MoTa;
                    db.SaveChanges();
                    kn = db.KyNangs.Include("Nhanviens").Single(k => k.MaKyNang == kn.MaKyNang);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult Xoa(string maKN)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var kn = db.KyNangs.Single(k => k.MaKyNang == maKN);
                    db.KyNangs.Remove(kn);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }
    }
}
